﻿using System;
using System.Collections.Generic;
using System.Data;
using MySql.Data.MySqlClient;
using l1cs.server.utils;

namespace l1cs.server.datatables
{
    public class DropItemTable
    {
        /// <summary>
        /// ドロップアイテムクラス？（76個しかリストがなく使用用途がわからない)
        /// </summary>
        private class dropItemData
        {
            public double dropRate = 1;
            public double dropAmount = 1;
        }

        private static Logger _log = Logger.getLogger( typeof( DropItemTable ).FullName );

        private static DropItemTable _instance;

        private readonly Dictionary<int, dropItemData> _dropItem = new Dictionary<int, dropItemData>();

        public static DropItemTable getInstance()
        {
            if ( _instance == null )
            {
                _instance = new DropItemTable();
            }
            return _instance;
        }

        private DropItemTable()
        {/*TTOK*/
            String sqlstr = "SELECT * FROM drop_item";
            using ( MySqlWrapper wrapper = new MySqlWrapper() )
            {
                foreach ( DataRow item in wrapper.GetDataTable(sqlstr).Rows )
                {
                    dropItemData data = new dropItemData();
                    int itemId = SqlUtils.toInt( item["item_id"] );
                    data.dropRate = Convert.ToDouble( item["drop_rate"] );
                    data.dropAmount = Convert.ToDouble( item["drop_amount"] );

                    _dropItem[itemId] = data;
                }
            }

            _log.config( "drop_item " + _dropItem.Count );
        }

        /// <summary>
        /// アイテムのドロップレートを取得する。?
        /// </summary>
        /// <param name="itemId">アイテムID</param>
        /// <returns></returns>
        public double getDropRate( int itemId )
        {/*TTOK*/
            dropItemData data;
            _dropItem.TryGetValue( itemId, out data );
            if ( data == null )
            {
                return 1;
            }
            return data.dropRate;
        }

        /// <summary>
        /// アイテムのドロップ数を取得する。？
        /// </summary>
        /// <param name="itemId">アイテムID</param>
        /// <returns></returns>
        public double getDropAmount( int itemId )
        {/*TTOK*/
            dropItemData data;
            _dropItem.TryGetValue( itemId, out data );
            if ( data == null )
            {
                return 1;
            }
            return data.dropAmount;
        }
    }
}
